﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Raven.Client.Document;
using Schwazzle.Core.Model;

namespace Schwazzle.Core.Services
{
    public class RavenFeedService : Schwazzle.Core.Services.IFeedService
    {
        DocumentStore _documentStore;
        public RavenFeedService()
        {
            _documentStore = new DocumentStore { Url = "http://vaiopony:8080" };
            _documentStore.Initialize();
        }

        public string CreateFeedEntry(string EntryText, string Username, DateTime FeedEntryDate)
        {
            string entryId;
            using (var session = _documentStore.OpenSession())
            {
                var entity = new FeedEntry { FeedEntryText = EntryText, Username = Username, EntryDate = FeedEntryDate };
                session.Store(entity);
                session.SaveChanges();
                entryId = entity.Id;
            }
            return entryId;
        }

        public IQueryable<FeedEntry> GetUsersFeedEntry(string Username)
        {

            using (var session = _documentStore.OpenSession())
            {
                // Filtering by string comparison on a property
                IQueryable<FeedEntry> results = from feed in session.Query<FeedEntry>()
                              where feed.Username == Username
                              select feed;
                return results;
            }
        }
    }
}
